﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Wams.Admin;

namespace Wams.Server
{
    public static class MessageExecutions
    {
        public static void Execute(this SyncContentMessage command)
        {
            if (!WamsCore.Sites.ContainsKey(command.SiteId)) WamsCore.LoadSettingsFile();
            if (!WamsCore.Sites.ContainsKey(command.SiteId)) return;
            WamsCore.Sites[command.SiteId].SyncContent();
        }

        public static void Execute(this SyncSettingsMessage command)
        {
            WamsCore.LoadSettingsFile();
            if (!WamsCore.Sites.ContainsKey(command.SiteId)) return;
            WamsCore.Sites[command.SiteId].SyncSettings();
        }

        public static void Execute(this RefreshResourceListMessage command)
        {
            WamsCore.LoadSettingsFile();
        }

        public static void Execute(this SetBusyStateMessage command)
        {
            if (WamsCore.InstanceState.IsBusy) Logger.Log(LogLevel.Warning, string.Format("Instance is busy. Setting busy state to {0}", command.IsBusy));
            else Logger.Log(LogLevel.Warning, string.Format("Instance is NOT busy. Setting busy state to {0}", command.IsBusy));
            WamsCore.InstanceState.IsBusy = command.IsBusy;
            WamsCore.InstanceState.Save();
        }

        public static void Execute(this PingMessage command)
        {
            WamsCore.InstanceState.Save();
        }

        public static void Execute(this RecycleMessage command)
        {
            WamsCore.RequestRecycle();
        }
        public static void Execute(this SyncCertificatesMessage command)
        {
            WamsCore.LoadSettingsFile();
            WamsCore.LoadCertificates();
        }
    }
}